package cn.edu.cdtu.healthclient.connect;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import cn.edu.cdtu.healthclient.domain.Doctor;
import cn.edu.cdtu.healthclient.utils.ConnectUtil;
import cn.edu.cdtu.healthclient.utils.ConnectUtil.HealthDoctorRegister;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class DoctorService {

	
/**
 * 
 * @param httpClient 传输所用
 * @param doWhat	得到医生的数据用来做什么的
 * @return	返回医生数据列表list
 * @throws Exception
 */
	
	public static List<Doctor> getJSONDoctors(HttpClient httpClient,String doWhat) 
			throws Exception {
		List<Doctor> doctors = null;
		Gson gson = new Gson();
		String path = ConnectUtil.HealthServerContect.SERVER_URL
				+ ConnectUtil.HealthServerContect.DOCTOR_SERVLET;
		HttpPost post = new HttpPost(path);
		List<NameValuePair> params= new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(HealthDoctorRegister.Option.DO_WHAT,doWhat));
		post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
		HttpResponse response = httpClient.execute(post);
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			String message = EntityUtils.toString(response.getEntity(),HTTP.UTF_8);
			doctors = gson.fromJson(message, 
					new TypeToken<List<Doctor>>(){}.getType());
		}
		return doctors;
	}
}
